const query = require('../db/mysql')
const {success, error} = require('../model/model')
class HomeController {
    // 获取商品列表 =？》 分页 模糊搜查  排序
    getShopList = async  (ctx) =>{
        // ?id=1
        const {pageSize, pageIndex, keyword, sortType,sortBy} = ctx.query;
        let sql = `select * from shops where 1=1 `
        let sqlCount = `select count(id) as total from shops where 1=1 `
        if(keyword){
            sql += `and title like "%${keyword}%" `
            sqlCount += `and title like "%${keyword}%" `
        }
        if(sortBy && sortType) {
            sql += `order by ${sortBy} ${sortType} `
        }
        if(pageIndex  && pageSize) {
            sql += `limit ${(pageIndex-1)*pageSize},${pageSize}`
        }
        const data = await query(sql) // 获取总数据
        const total = await query(sqlCount) // 获取总数量
        ctx.body = success({
            data,
            total: total[0].total
        })
    }
    // 获取商品详情
    getShopDetail = async (ctx) => {
        // /list/:id /list/1 ctx.params
        const {id} = ctx.params;
        let sql = `select * from shops where id="${id}"`
        const data = await query(sql)
        ctx.body = success(data)
    }
}

module.exports = new HomeController()